package com.wind.trademark.client.dao.impl;


// default package

import java.util.List;
import java.util.Map;

import org.hibernate.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.simple.ParameterizedBeanPropertyRowMapper;
import org.springframework.stereotype.Repository;

import com.wind.trademark.client.dao.CompanyDao;
import com.wind.trademark.client.model.CtCompany;
import com.wind.trademark.client.model.CtContactor;
import com.wind.trademark.common.dao.BaseDao;
import com.wind.trademark.common.vo.ComboBean;
import com.wind.trademark.common.vo.PageBean;
import com.wind.trademark.common.vo.PageParam;





/**
 * @author  <a href="mailto:xmufeng@163.com">Wind Wu</a>
 *
 */
@Repository
public class CompanyDaoImpl implements CompanyDao{
	@Autowired
	private BaseDao baseDao;

	@Override
	public PageBean queryCompany(PageParam pageParam,  Map<String,Object> params) {
		
		String sql= CompanySql.queryCompanySQL(params);
		PageBean pageBean=baseDao.queryPageBeanAndMapping(sql, pageParam,CtCompany.class, params);
		return pageBean;

	}

	
	@Override
	@SuppressWarnings("unchecked")
	public List<CtCompany> getCtCompanyByCompanyName(String compName) {
		String hql="SELECT ctCompany FROM CtCompany ctCompany  WHERE ctCompany.cnName LIKE :compName";
		 Query  query=baseDao.getHibernateTemplate().getSessionFactory().getCurrentSession().createQuery(hql);
		 query.setString("compName", '%'+compName+'%');
		return query.list();
	}


	@Override
	public List<ComboBean> goolgeCtCompanyByCompanyName(Map<String, Object> params) {
		String sql= CompanySql.getGoolgeCtCompanyByCompanyNameSQL(params);
		RowMapper<ComboBean> rowMapper = ParameterizedBeanPropertyRowMapper.newInstance(ComboBean.class);
		List<ComboBean> result = baseDao.getNamedParameterJdbcTemplate().query(sql,params, rowMapper);
		return result;
	}


	@Override
	public PageBean queryContactor(PageParam pageParam,
			Map<String, Object> params) {
		String sql= CompanySql.queryContactorSQL(params);
		PageBean pageBean=baseDao.queryPageBeanAndMapping(sql, pageParam,CtContactor.class, params);
		return pageBean;
	}


	@Override
	public void deleteContactors(List<Long> ids) {
		String sql= CompanySql.deleteContactorsSQL();
		baseDao.getNamedParameterJdbcTemplate().update(sql,new MapSqlParameterSource().addValue("ids",ids));

	}


	@Override
	public void updateContactorUnDefault(Long compId) {
		String sql= CompanySql.updateContactorUnDefaultSQL();
		baseDao.getNamedParameterJdbcTemplate().update(sql,new MapSqlParameterSource().addValue("compId",compId));
	}

}